package com.douwong.student.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.douwong.student.R;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.contant.NetConfig;
import com.douwong.student.model.StatusCode;
import com.douwong.student.model.SubjectObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;

/**
 * 
 * @author 搜索类
 *
 */
public class CommSearch {
	private AlertDialog dialog;
	private String startTime = null;
	public String getStartTime() {
		return startTime;
	}
	public void setStartTime(String startTime, String action) {
		this.startTime = startTime;
		Intent intent = new Intent();
		intent.setAction(action);
		intent.putExtra("start_time", startTime);
		intent.putExtra("subjectId", subjectId);
		intent.putExtra("end_time", endTime);
		context.sendBroadcast(intent);
	}

	private String endTime = null;
	private String[] subjects = null;
	private String subjectId = null;
	private Context context;
	private int yearInt = 2015;
	private int monthInt = 5;
	private int dayInt = 20;
	private DatePicker datePicker;
	private ArrayList<SubjectObject> list = new ArrayList<SubjectObject>();

	public CommSearch(Context context) {
		this.context = context;
		getSubjectList();
	}
//显示科目
	public void showSubject(final TextView currentSubject, final LinearLayout select_time, final String action) {
		if (dialog == null) {
			if (list.size() > 0) {
				subjects = new String[list.size() + 1];
				subjects[0] = "全部";
				for (int i = 0; i < list.size(); i++) {
					subjects[i + 1] = list.get(i).getSubjectName();
				}
				if (subjects.length > 0) {
					dialog = new AlertDialog.Builder(context).setItems(subjects, new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							currentSubject.setText(subjects[which]);
							if (which == 0) {
								select_time.setVisibility(View.GONE);
								subjectId = null;
							} else {
								subjectId = String.valueOf(list.get(which - 1).getSubjectId());
							}
							Intent intent = new Intent();
							intent.setAction(action);
							intent.putExtra("start_time", startTime);
							intent.putExtra("subjectId", subjectId);
							intent.putExtra("end_time", endTime);
							context.sendBroadcast(intent);
						}
					}).create();
				}

			}
		}
		if (dialog != null && !dialog.isShowing()) {
			dialog.show();
		}
	}

	// 得到科目列表
	private void getSubjectList() {
		RequestParams params = new RequestParams();
		if (BaseApplication.sUserBean != null && BaseApplication.sUserBean.getSchoolbean() != null) {
			
			params.addBodyParameter("schoolId", BaseApplication.sUserBean.getSchoolbean().getId() + "");
			new HttpUtils().send(HttpMethod.POST, NetConfig.SUBJECT_LIST, new RequestCallBack<String>() {

				@Override
				public void onFailure(HttpException arg0, String arg1) {
//					Toast.makeText(context, arg1, Toast.LENGTH_LONG).show();
				}

				@Override
				public void onSuccess(ResponseInfo<String> arg0) {
					StatusCode<String> sc = JSON.parseObject(arg0.result, new TypeReference<StatusCode<String>>() {

					});
					if (sc != null && sc.isFlag()) {
						ArrayList<SubjectObject> temp = (ArrayList<SubjectObject>) JSON.parseArray(sc.getData(),
								SubjectObject.class);
						if (temp != null && temp.size() > 0) {
							list.addAll(temp);
							int length = list.size();
							for (int i = 0; i < length - 1; i++) {
								String temp2 = list.get(i).getSubjectName();
								for (int j = i + 1; j < length; j++) {
									if (temp2.equals(list.get(j).getSubjectName())) {
										list.remove(j);
										j--;
										length--;
									}
								}
							}
							Collections.sort(list);
						}
					}
				}
			});
		}

	}

	// 时间
	public void showTimeWindow(final LinearLayout time_ll,final TextView select_time, final String action) {

		View v1 = LayoutInflater.from(context).inflate(R.layout.pop_times, null);
		final TextView times = (TextView) v1.findViewById(R.id.test);
		new AlertDialog.Builder(context).setTitle("请选择开始时间").setIcon(android.R.drawable.ic_dialog_info).setView(v1)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						int monthShow = monthInt + 1;
						if (monthShow >= 10) {
							if (dayInt > 10) {
								startTime = yearInt + "-" + monthShow + "-" + dayInt;
							} else {
								startTime = yearInt + "-" + monthShow + "-0" + dayInt;
							}
						} else {
							if (dayInt > 10) {
								startTime = yearInt + "-0" + monthShow + "-" + dayInt;
							} else {
								startTime = yearInt + "-0" + monthShow + "-0" + dayInt;
							}
						}
						Intent intent = new Intent();
						intent.setAction(action);
						intent.putExtra("start_time", startTime);
						intent.putExtra("subjectId", subjectId);
						intent.putExtra("end_time", endTime);
						context.sendBroadcast(intent);
						select_time.setText(startTime);
						time_ll.setVisibility(View.VISIBLE);
					}
				}).setNegativeButton("取消", null).show();
		datePicker = (DatePicker) v1.findViewById(R.id.dp);
		datePicker.init(yearInt, monthInt, dayInt, new OnDateChangedListener() {
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				// 获取一个日历对象，并初始化为当前选中的时间
				yearInt = year;
				monthInt = monthOfYear;
				dayInt = dayOfMonth;
				Calendar calendar = Calendar.getInstance();
				times.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
				SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  ");
			}
		});
	}
}
